package com.ruoyi.monitor.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.monitor.domain.SysMsg;
import com.ruoyi.monitor.domain.SysMsgModel;
import com.ruoyi.monitor.service.ISysMsgModelService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.monitor.service.ISysMsgService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 邮箱发送Controller
 * 
 * @author xzx
 * @date 2023-02-22
 */
@RestController
@RequestMapping("/monitor/Msg")
public class SysMsgController extends BaseController
{
    @Autowired
    private ISysMsgService sysMsgService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysMsgModelService modelService;
    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    ThreadPoolTaskExecutor pool;

    /**
     * 查询邮箱发送列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:Msg:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMsg sysMsg)
    {
        startPage();
        List<SysMsg> list = sysMsgService.selectSysMsgList(sysMsg);
        list.forEach(msg -> {
            SysMsgModel model = modelService.getById(msg.getModelId());
            msg.setModel(model);
            SysUser user = sysUserService.selectUserById(msg.getUserId());
            msg.setUser(user);
        });
        return getDataTable(list);
    }

    /**
     * 导出邮箱发送列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:Msg:export')")
    @Log(title = "邮箱发送", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMsg sysMsg)
    {
        List<SysMsg> list = sysMsgService.selectSysMsgList(sysMsg);
        ExcelUtil<SysMsg> util = new ExcelUtil<SysMsg>(SysMsg.class);
        util.exportExcel(response, list, "邮箱发送数据");
    }

    /**
     * 获取邮箱发送详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:Msg:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        SysMsg sysMsg = sysMsgService.selectSysMsgById(id);
        SysUser user = sysUserService.selectUserById(sysMsg.getUserId());
        sysMsg.setUser(user);
        return success(sysMsg);
    }

    /**
     * 新增邮箱发送
     */
    @PreAuthorize("@ss.hasPermi('monitor:Msg:add')")
    @Log(title = "邮箱发送", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMsg sysMsg)
    {
        return toAjax(sysMsgService.insertSysMsg(sysMsg));
    }

    /**
     * 修改邮箱发送
     */
    @PreAuthorize("@ss.hasPermi('monitor:Msg:edit')")
    @Log(title = "邮箱发送", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMsg sysMsg)
    {
        return toAjax(sysMsgService.updateSysMsg(sysMsg));
    }

    /**
     * 删除邮箱发送
     */
    @PreAuthorize("@ss.hasPermi('monitor:Msg:remove')")
    @Log(title = "邮箱发送", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(sysMsgService.deleteSysMsgByIds(ids));
    }
}
